<template>
  <header 
    class="header" 
    :class="{ 'header--scrolled': isScrolled }"
  >
    <div class="banner">
      <div 
        class="banner-content" 
        :class="{ 'banner-content--hidden': isScrolled && isScrollingDown }"
      >
        <div class="menu-btn" @click="toggleMobileMenu">
          <img src="/src/assets/images/menu.png" alt="menu" class="menu">
          MENU
        </div>
        <div class="logo-container">
          <img src="/src/assets/images/logo.png" @click="navigateTo('/')" alt="YOUNGYUAN" class="logo">
        </div>
        <div class="search-container">
          <img src="/src/assets/images/search.png" alt="search" class="search-icon">
          <a-input type="text" class="search-input" placeholder="搜索"></a-input>
        </div>
      </div>
      <div class="category-nav">
        <div 
          v-for="(link, index) in navLinks" 
          :key="index" 
          class="category-item"
          :class="{ 'category-item--active': link.path === $route.path }"
          @click="navigateTo(link.path)"
        >
          {{ link.name }}
        </div>
      </div>
    </div>

    <!-- antd抽屉菜单 -->
    <a-drawer
      placement="left"
      :open="isMobileMenuOpen"
      :closable="false"
      :width="700"
      @close="toggleMobileMenu"
      :maskStyle="{ backgroundColor: 'rgba(0, 0, 0, 0.75)' }"
    >
      <div class="drawer-content">
        <div class="custom-close-btn" @click="toggleMobileMenu"><CloseOutlined style="font-size: 16px; margin-right: 6px;" /> 关闭</div>
        <div class="drawer-nav">
          <div class="drawer-nav-item">礼品指南 <RightOutlined class="right-arrow" /></div>
          <div class="drawer-nav-item">至尊系列 <RightOutlined class="right-arrow" /></div>
          <div class="drawer-nav-item">黑晶 Black Crystal <RightOutlined class="right-arrow" /></div>
          <div class="drawer-nav-item">紫晶 Amethyst <RightOutlined class="right-arrow" /></div>
          <div class="drawer-nav-item">瑰晶 Rose Crystal <RightOutlined class="right-arrow" /></div>
          <div class="drawer-nav-item">赠礼服务 <RightOutlined class="right-arrow" /></div>
        </div>
        <div class="drawer-divider"></div>
        <div class="drawer-service">
          <div class="drawer-nav-item">客户服务</div>
          <div class="drawer-nav-item">025-58328217</div>
          <div class="drawer-nav-item">招商代理</div>
        </div>
      </div>
    </a-drawer>
  </header>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { CloseOutlined, RightOutlined, SearchOutlined } from '@ant-design/icons-vue'
// 不需要额外导入，因为已在main.js中全局注册了Ant Design Vue

const router = useRouter()
const isScrolled = ref(false)
const isMobileMenuOpen = ref(false)
const isScrollingDown = ref(false)
const lastScrollPosition = ref(0)

const navLinks = [
  { name: '品牌BRAND', path: '/brand' },
  { name: '黑晶 BLACK CRYSTAL', path: '/black-crystal' },
  { name: '紫晶 AMETHYST', path: '/amethyst' },
  { name: '瑰晶 ROSE CRYSTAL', path: '/rose-crystal' },
  { name: '国际研究中心', path: '/pilgrim-center' }
]

const handleScroll = () => {
  const currentScrollPosition = window.scrollY
  isScrolled.value = currentScrollPosition > 50
  
  // 确定滚动方向
  isScrollingDown.value = currentScrollPosition > lastScrollPosition.value
  
  // 更新上次滚动位置
  lastScrollPosition.value = currentScrollPosition
}

const toggleMobileMenu = () => {
  isMobileMenuOpen.value = !isMobileMenuOpen.value
}

const navigateTo = (path) => {
  router.push(path)
}

onMounted(() => {
  window.addEventListener('scroll', handleScroll)
})

onUnmounted(() => {
  window.removeEventListener('scroll', handleScroll)
})
</script>

<style scoped>
.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  background-color: transparent;
  transition: var(--transition);
  width: 100%;
}

.search-icon {
  display: inline-block;
  vertical-align: middle;
  margin-right: 6px;
}

.header--scrolled {
  background-color: rgba(255, 255, 255, 0.9);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
}

.banner {
  width: 100%;
  background-color: #f8f9fa;
}

.banner-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 67px;
  padding: 0 40px;
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.banner-content--hidden {
  transform: translateY(-100%);
}

.menu-btn {
  display: flex;
  align-items: center;
  font-size: 14px;
  cursor: pointer;
  color: #616161;
}

.menu {
  height: 12px;
  margin-right: 8px;
}

.logo-container {
  text-align: center;
}

.logo {
  height: 54px;
  cursor: pointer;
}

.search-container {
  position: relative;
}

.search-input {
  border: none;
  border-bottom: 1px solid #ddd;
  background-color: transparent;
  border-radius: 0;
  padding: 5px 10px;
  font-size: 14px;
  width: 120px;
  &:focus {
    box-shadow: none;
  }
}

.category-nav {
  display: flex;
  justify-content: center;
  padding: 10px 0;
  padding-bottom: 0;
  border-top: 1px solid #eee;
  border-bottom: none;
  position: relative;
  transition: margin-top 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  line-height: 25px;
}

.banner-content--hidden + .category-nav {
  margin-top: -67px;
  border-top: none;
}

.category-item {
  padding: 0;
  margin: 0 20px;
  padding-bottom: 4px;
  font-size: 14px;
  color: #616161;
  cursor: pointer;
  position: relative;
  transition: color 0.3s ease;
}

.category-item::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 1px;
  background-color: #000;
  transform: scaleX(0);
  transform-origin: left;
  transition: transform 0.3s ease;
}

.category-item--active::after {
  transform: scaleX(1);
}

.category-item:hover::after {
  transform: scaleX(1);
}

.category-item:hover {
  color: #333;
}

/* 抽屉内容样式 */
.drawer-content {
  padding: 20px 0;
}

.drawer-nav-item {
  padding: 12px 24px;
  font-size: 14px;
  color: #828282;
  cursor: pointer;

  .right-arrow {
    display: none;
    float: right;
  }

  &:hover {
    color: #000;
    .right-arrow {
      display: block;
    }
  }
}

.drawer-nav-item:hover {
  background-color: #f9f9f9;
}

.drawer-divider {
  height: 1px;
  background-color: #ddd;
  margin: 20px 0;
}

.drawer-service {
}

.drawer-service-item {
  padding: 8px 0;
  font-size: 14px;
  color: #666;
}

/* 自定义关闭按钮样式 */
.custom-close-btn {
  font-size: 14px;
  color: #333;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 20px 20px 15px;
  border-bottom: 1px solid #eee;
  width: 100%;
  padding-top: 0;
}

.custom-close-btn:hover {
  color: #666;
}

@media (max-width: 768px) {
  .banner-content {
    padding: 0 20px;
  }
  
  .category-nav {
    display: none;
  }
  
  .menu-btn {
    margin-right: 10px;
  }
}
</style> 